<?php

class Frmcreator_Model_DbTable_Elements extends Engine_Db_Table {

    protected $_rowClass = 'Frmcreator_Model_Element';

    public function createElement($data, $form_id) {
        $optionTable = Engine_Api::_()->getDbtable('options', 'frmcreator');
        
        $form = Engine_Api::_()->getItem('frmcreator_form', $form_id);
        
        $index = 1;
        foreach ($data as $row) {
            // get element settings
            $settings = $attributes = array();
            foreach ($row[1] as $ele) {
                if ($ele['name'] == 'option[]') {
                    $settings['option'][] = $ele['value'];
                } else if ($ele['name'] != 'title' && $ele['name'] != 'required' && $ele['name'] != 'description') {
                    $attributes[$ele['name']] = $ele['value'];
                } else {
                    $settings[$ele['name']] = $ele['value'];
                }
            }

            $label          = isset($settings['title']) && $settings['title'] != '' ? urldecode($settings['title']) : '';
            $description    = isset($settings['description']) && $settings['description'] != '' ? urldecode($settings['description']) : '';
            $required       = isset($settings['required']) && $settings['required'] ? true : false;

            // check element type 
            $para = explode("_", $row[0]);
            
            if($para[2] == "title"){
                $form->title = $label;
            } else if($para[2] == "description"){
                $form->description = $description;
            }
            
            $type = Engine_Api::_()->frmcreator()->getMapElement($para[2]);
                       
            // setup general element settings
            $element = array('element_id' => '',
                'form_id' => $form_id,
                'element_type' => $type,
                'label' => $label,
                'description' => $description,
                'order' => $index++,
                'required' => $required,
                'attributes' => serialize($attributes),
                'layout_type' => $row[0]);
            
            
            //insert element
            $this->insert($element);
            $ele_id = $this->getAdapter()->lastInsertId();

            // insert to table engine4_frmcreator_options

            if ($ele_id && isset($settings['option'])) {
                foreach ($settings['option'] as $row) {
                    $option = array('element_id' => $ele_id, 'label' => $row);
                    $optionTable->insert($option);
                }
            }
        }
        
        if($form){
            $form->save();
        }
    }

    public function getElement($form_id) {  
        $optionTable = Engine_Api::_()->getDbtable('options', 'frmcreator');
        $select     = $this ->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
                        ->setIntegrityCheck(false)
                        ->where('form_id = ?', $form_id);

        $elements   =  $this->fetchAll($select)->toArray();
        $result     = array();
        foreach($elements as $ele ){
            if( in_array($ele['element_type'],array('multiselect','select','checkbox','radio'))){
                $options    = $optionTable->fetchAll($optionTable->select()->where('element_id = ?', $ele['element_id']))->toArray();
                $result[]   = array_merge($ele, array('option'=>$options));
            }else{
                $result[]   = $ele;
            }
            
        }
        return $result; 
    }

}
